package org.road0329.dlt698.asn1.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;

import java.io.IOException;
import java.io.InputStream;

public class AxdrBoolean {
    private boolean val = false;
    private byte[] dataCode = null;

    public AxdrBoolean() {
    }

    public boolean isVal() {
        return val;
    }

    public byte[] getDataCode() {
        return dataCode;
    }

    public void setVal(boolean val) {
        this.val = val;
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        encode(revOStream);
        this.dataCode = revOStream.toByteArray();
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        if (this.dataCode != null) {
            output.writeByte(this.dataCode);
        } else if (this.val) {
            output.write(0x01);
        } else {
            output.write(0x00);
        }
        return 1;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        if (input.available() == 0) {
            return 0;
        }
        this.val = input.read() != 0x00;
        return 1;
    }

    public boolean getVal() {
        return this.val;
    }

    @Override
    public String toString() {
        return Boolean.toString(this.val);
    }
}



